<?php
# autoHTML: the dynamic PHP5 library for fast website prototyping
# http://code.google.com/p/autohtml/
#
# Copyright (c) 2010, Shu Zong Chen
# http://freelanceDreams.com/
# All rights reserved. 
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met: 
# 
#  * Redistributions of source code must retain the above copyright notice, 
#    this list of conditions and the following disclaimer. 
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution. 
#  * Neither the name of autoHTML nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission. 
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

class AUTOHTML_ASSET_MANAGER{
	public $ASSETS = null;
	function __construct(){
		$this->ASSETS = array();
		$new_mem = new AUTOHTML_ASSET_MEMORY();
		$this->_attachAsset($new_mem);
	}
	function attachAsset($asset){
		switch ($asset['type']){
			case 'memory':
				$as = new AUTOHTML_ASSET_MEMORY();
				return $this->_attachAsset($as);
			case 'filesystem':
				$as = new AUTOHTML_ASSET_FILESYSTEM($asset['resource']);
				return $this->_attachAsset($as);
		}
	}
	function _attachAsset($asset){
		$this->ASSETS[] = $asset;
		return 1;
	}
	function attach($target, $element, $override){
		$this->ASSETS[0]->attach($target, $element, $override);
	}
	function fetch($target){
		$id = $this->roll_call($target);
		if ($id !== null){
			return $this->ASSETS[$id]->fetch($target);
		}
	}
	function render($target){
		$id = $this->roll_call($target);
		if ($id !== null){
			echo $this->ASSETS[$id]->fetch($target);
		}
	}
	function roll_call($target){
		foreach ($this->ASSETS as $key=>$asset){
			if ($asset->roll_call($target)){
				return $key;
			}
		}
	}
}
abstract class AUTOHTML_ASSET_GENERIC{
	protected $RESOURCE = null;
	protected $HANDLE = null;

	function __construct($resource = null){
		$this->RESOURCE = $resource;
		$this->init_resource();
	}
	function attach($target, $resource, $override){
		return false;
	}
	function fetch($target){
		return false;
	}
	function roll_call($id){
		return false;
	}
	abstract function init_resource();
}

class AUTOHTML_ASSET_MEMORY extends AUTOHTML_ASSET_GENERIC{
	function init_resource(){
		$this->ELEMENTS = array();
	}
	function attach($target, $resource, $override){
		$this->ELEMENTS[$target] = $resource;
	}
	function fetch($target){
		if (isset($this->ELEMENTS[$target])){
			return $this->ELEMENTS[$target];
		}
		return null;
	}
	function roll_call($target){
		return isset($this->ELEMENTS[$target]);
	}
}

class AUTOHTML_ASSET_FILESYSTEM extends AUTOHTML_ASSET_GENERIC{
	function init_resource(){
	}
	function fetch($target){
		$fn = path_join($this->RESOURCE, $target);
		if (file_exists($fn)){
			ob_start();
			include ($fn);
			return ob_get_clean();
		}
		return false;
	}
	function roll_call($target){
		if (file_exists(path_join($this->RESOURCE, $target))){
			return true;
		}
		return false;
	}
}